import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'

import './assets/css/index.less'

//axios添加请求,响应拦截器
axios.interceptors.request.use(config => {
	console.log(config);
	//TODO 配合服务端做无状态验证的时候 处理接口权限问题
	if (config.url != '/api/login') {
		//在请求头添加数据 或者其它的处理
		config.headers.auth = 'CDGHKFDSHKFGBSDHBFHWMKGBFSGHKGHKGBHK';
	}
	return config;
}, error => Promise.reject(error));

axios.interceptors.response.use(response => response.data, error => Promise.reject(error));

Vue.prototype.$axios = axios;

Vue.config.productionTip = false

//定义路由守卫 处理登陆跳转问题
router.beforeEach((to, from, next) => {
	console.log('router gard...',to);
	if (to.meta.requireAuth) { //需要登录
		if (sessionStorage.getItem('accessToken')) { //已登录
			next();
		} else { //未登陆->首页
			next({
				name: 'home'
			});
		}
	} else { //无需登录
		next();
	}	
});

router.afterEach((to,from)=>{
	//TODO 可以结合beforeEach做 路由切换的loading 效果,loading控制的变量可以放在store中
	/** 
	 *  store.commit('updateLoadingStatus', {isLoading: false})
	 */
});

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')